<template>
  <div class="product">
    <div class="search">
      <el-input v-model="ipt" placeholder="请输入商品名"></el-input>
      <el-button type="primary" @click="searchHandler">搜索</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="创建日期" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.createAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品图片" width="180">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.images"
            fit="cover"
          ></el-image>
          <!-- <span style="margin-left: 10px">{{ scope.row.images }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="商品描述" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.desc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品单价" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品状态" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.productStatus }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品分类" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品库存" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      ipt: "",
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
      // console.log(row.productId);
      this.$router.push({
        path: "updateproduct",
        query: { productId: row.productId },
      });
      this.$store.commit("editFun", {
        editInfo: row,
      });
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.nextTick().then(function () {
            this.axios({
              method: "post",
              url: "http://localhost:3000/deleteproduct",
              data: {
                productId: row.productId,
              },
            }).then((res) => {
              this.$message({
                type: "success",
                message: res.data.msg,
              });
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    searchHandler() {
      this.axios({
        method: "post",
        url: "http://localhost:3000/searchproduct",
        data: {
          ipt: this.ipt.trim(),
        },
      }).then((res) => {
        console.log(res);
        if (res.data.status == 405) {
          this.tableData = res.data.products;
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
  },
  created() {
    this.axios.get("http://localhost:3000/product").then((res) => {
      console.log(res);
      if (res.data.status == 405) {
        this.tableData = res.data.productArr;
      } else {
        this.$message({
          type: "warning",
          message: res.data.msg,
        });
      }
    });
  },
};
</script>

<style lang="less" scoped>
.product {
  box-sizing: border-box;
  padding: 20px 0 0 20px;

  .search {
    width: 500px;
    display: flex;
    margin-bottom: 50px;
  }
}
</style>